﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.IO;

namespace Server {
    class Program {
        static void Main(string[] args) {
            using (Socket echoListener = new Socket(
                AddressFamily.InterNetwork,
                SocketType.Stream,
                ProtocolType.Tcp)) {
                    IPEndPoint echoEndPoint = new IPEndPoint(IPAddress.Any, 13);
                    echoListener.Bind(echoEndPoint);
                    echoListener.Listen(20);
                    string data;
                    while (true) {
                        Console.WriteLine("Wainting");
                        Socket incommingConnection = echoListener.Accept();
                        using (NetworkStream connStream = new NetworkStream(incommingConnection, true))
                        using (StreamReader reader = new StreamReader(connStream)) {
                            Console.WriteLine("Accepted conn");
                            data = reader.ReadLine();
                            Console.WriteLine("[server] received: {0}", data);
                            StreamWriter writer = new StreamWriter(connStream);
                            writer.WriteLine(data);
                            writer.Flush();
                        }
                    }
            }
        }
    }
}
